package com.controller;

import com.dto.JsonResult;
import com.entity.CarInfo;
import com.service.CarService;
import com.utils.TimeUtils;
import com.vo.AddCarVo;
import com.vo.CarVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Yadasu
 * @date 2020/11/16 15:06
 */
@RestController
@RequestMapping("carInfos")
public class CarController {
    @Autowired
    private CarService carService;

    /**
     * 查询所有的车辆信息
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public JsonResult get() {
        return JsonResult.buildSuccess(0, carService.findAllCarInfo());
    }

    /**
     * 删除车辆信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public JsonResult del(@PathVariable("id") Integer id) {
        return JsonResult.buildSuccess(200, carService.delCar(String.valueOf(id)));
    }

    /**
     * 取车
     *
     * @param carVo
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public JsonResult update(@RequestBody CarVo carVo) {
        return JsonResult.buildSuccess(200, carService.getCar(carVo));

    }

    @RequestMapping(value = "",method = RequestMethod.PUT)
    public JsonResult add(@RequestBody AddCarVo addCarVo){
        CarInfo carInfo = addCarVo.getCarInfo();
        System.out.println(carInfo);
        Date park_start = addCarVo.getDate();
        System.out.println(park_start);
        Date date = TimeUtils.getDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(park_start));
        carInfo.setPark_start(date);
        return JsonResult.buildSuccess(200,carService.add(carInfo));
    }
}
